var prefix = "/user";
var treeGrid ="";
//$(function(){
	
	/*var data = [
        {
            id: '1',
            name: "aa",
            desc: "这是一个描述",
            parentId:null,
        },{
            id: '11',
            name: "aa_sub1",
            desc: "这是一个描述_sub1",
            parentId:'1',
        },{
            id: '111',
            name: "aa_sub1_sub2",
            desc: "这是一个描述_sub1_sub2" ,
            parentId:'11',
        },
        {
            id: '2',
            name: "bb",
            desc: "这是一个描述",
            parentId:null,
        },
        {
            id: '22',
            name: "bb_sub1",
            desc: "这是一个描述_sub1",
            parentId:'2',
        },{
            id: '23',
            name: "bb_sub2",
            desc: "这是一个描述_sub2",
            parentId:'2',
        },{
            id: '233',
            name: "bb_sub2_sub2",
            desc: "这是一个描述_sub2_sub2",
            parentId:'23',
        },{
            id: '2333',
            name: "bb_sub2_sub2_sub2",
            desc: "这是一个描述_sub2_sub2_sub2",
            parentId:'233',
        },{
            id: '23333',
            name: "bb_sub2_sub2_sub23",
            desc: "这是一个描述_sub2_sub2_sub2",
            parentId:'233',
        },{
            id: '233333',
            name: "bb_sub2_sub2_sub233",
            desc: "这是一个描述_sub2_sub2_sub2",
            parentId:'233',
        },{
            id: '24',
            name: "bb_sub3",
            desc: "这是一个描述_sub3",
            parentId:'2',
        },
    ]*/
	
	/*$.ajax({
        type: "post",
        url: prefix+"/menulist",
        async:false,
        success: function(data){
        	console.log(data);
        }
    });*/
	
	/*treegrid config*/
	var config = {
			id: "datatables-organization",//table的id
			width: "100%",
			renderTo: "dataTables-organization_wrapper",//渲染table的容器对象id
			headerAlign: "left",
			// headerHeight: "38",
			dataAlign: "left",
			indentation: "20",//缩进
			folderOpenIcon: "plug-in/jqTreeGrid/img/collapse.png",//展开的图标
			folderCloseIcon: "plug-in/jqTreeGrid/img/expand.png",//关闭的图标
			// defaultLeafIcon: "img/branch_icon.png",//叶子节点的图标
			hoverRowBackground: "true",//鼠标悬停是否显示背景色
			folderColumnIndex: "1",//层次缩进
			// itemClick: "itemClickEvent",//点击一行的事件处理函数
			columns:[//列名,表头和数据的对齐方式，宽度（可以是数值，也可以是百分比），自定义处理函数等
				{headerText: "", headerAlign: "left", dataAlign: "left", width: "10"},
				{headerText: "菜单名称", dataField: "name", headerAlign: "left", handler: "customOrgName",width:""},
				{headerText: "图标", dataField: "icon", headerAlign: "left", dataAlign: "left", width: ""},
				{headerText: "排序", dataField: "order", headerAlign: "left", dataAlign: "left", width: "70"},
				{headerText: "地址", dataField: "url", headerAlign: "left", dataAlign: "left", width: "300"},
				{headerText: "操作", headerAlign: "left", dataAlign: "left", width: "", handler: "customLook"}
			],
			url:prefix+"/menulist",
			method:"post"
		};
	
	var treeGrid = new TreeGrid(config);
	treeGrid.show();
	
	function customLook(){
//		var laybtn="<input type='button' class='layui-btn layui-btn-sm' value='编辑'/>"
		var see ="<input type='button' class='layui-btn layui-btn-sm btn-info see' value='查看'/>&nbsp";
		var edit ="<input type='button' class='layui-btn layui-btn-sm btn-warning edit' value='修改'/>&nbsp";
		var addchildren ="<input type='button' class='layui-btn layui-btn-sm btn-primary  layui-btn-normal addchildren' value='添加子组织'/>&nbsp;";
		var del="<input type='button' class='layui-btn btn-danger layui-btn-sm  layui-btn-danger del' value='删除'/>";
		
		return /*see+*/edit+addchildren+del;
	}
	
	function customOrgName(row, col){
		var name = row[col.dataField] || "";
		return name;
	};
	
	$(function(){
		layui.use("layer",function(){
			var layer=layui.layer;
			
			/*$(".see").on("click", function(){
				var obj=$(this).parent().parent().attr("data")
				obj= TreeGrid.strTojson(obj);
				layer.open({
					type : 2,
					title : '查看',
					maxmin : true,
					shadeClose : false, // 点击遮罩关闭层
					area : [ '600px', '520px' ],
					content : prefix + '/adduser',// 
					end: function(){
						$('#exampleTable').bootstrapTable('refresh');//刷新table
					}
				});
			})*/
			
			$(".edit").on("click", function(){
				var obj=$(this).parent().parent().attr("data")
				obj= TreeGrid.strTojson(obj);
				layer.open({
					type : 2,
					title : '编辑',
					maxmin : true,
					shadeClose : false, // 点击遮罩关闭层
					area : [ '600px', '520px' ],
					content : prefix + '/editmenu?id='+obj.id,// 
					end: function(){
						treeGrid.refresh();
					}
				});
				
			})
			
			$(".addchildren").click(function(){
				var obj=$(this).parent().parent().attr("data")
				obj= TreeGrid.strTojson(obj);
				layer.open({
					type : 2,
					title : '添加子菜单',
					maxmin : true,
					shadeClose : false, // 点击遮罩关闭层
					area : [ '600px', '520px' ],
					content : prefix + '/addchildrendmenu?id='+obj.id,// 
					end: function(){
						treeGrid.refresh();
					}
				});
			})
			
			/*删除*/
			$(".del").click(function(){
				var obj=$(this).parent().parent().attr("data")
				objdata= TreeGrid.strTojson(obj);
				var objimg=$(this).parent().parent().find("img[data-folder='Y']");
				if(objimg.length > 0){
					layer.msg("该菜单有下级菜单，不可直接删除");
					return;
				}
				layer.confirm("确认要删除选中的数据吗?", {
					btn : [ '确定', '取消' ]
				// 按钮
				}, function() {
					$.ajax({
						type : 'POST',
						url : prefix + '/deletemenu?id='+objdata.id,
						beforeSend: function(request) {
							request.setRequestHeader("token", getCookie('token'));//添加token
						},
						success : function(r) {
							if (r.code == 0) {
								layer.msg(r.msg);
							} else {
								layer.msg(r.msg);
							}
							treeGrid.refresh();//刷新table
						}
					});
				}, function() {});
			})
				
//			})
			
		})
	})
	
	function add(){
		layui.use("layer",function(){
			var layer=layui.layer;
			layer.open({
				type : 2,
				title : '添加',
				maxmin : true,
				shadeClose : false, // 点击遮罩关闭层
				area : [ '600px', '520px' ],
				content : prefix + '/addmenu',// 
				end: function(){
					treeGrid.refresh();//刷新table
				}
			});
		})
	}
	
	
	
	/*$('#tree_table').bootstrapTable({
		method: 'post',
        class: 'table table-hover table-bordered',
        url:prefix+"/menulist",
         contentType: 'application/x-www-form-urlencoded; charset=utf-8',
//        data: data,
        sidePagination: 'server',
        pagination: false,
        treeView: true,
        treeId: "id",
        treeField: "name",
        rowAttributes: function (row, index) {
            return {
                xx:index
            };
        },
        queryParams : function(params) {
			var param = {
				roleId : 'xx'
			};
			return param;
		},
        columns: [{
            checkbox:true
        },{
            field: 'name',
            title: '名称',
        },
        {
            field: 'url',
            title: '详情',
        },
        ]
    });*/
//})